---
title: "QCA Rel Reg - S2 - QCA - Outcome 2 - NXX"
author: "Sannehag & Ettensperger"
date: '`r Sys.Date()`'
output:
  html_document: default
  pdf_document: default
---

<style type="text/css">
.main-container {
  max-width: 1800px;
  margin-left: auto;
  margin-right: auto;
}
</style>

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

## set working directory & load data

```{r}
setwd("E:/Fortis/Workspace/Research Article - QCA Religious Regulation")
# setwd("C:/Users/fe300/Desktop/Research Article - Reg Rel QCA")


QCA.Data <- read.csv("Data_QCA2_nxx.csv")
```

#Load packages for QCA

```{r}
library(QCA)
library(SetMethods)
library(ggplot2)
```

```{r}
# set names for truth table
QCA.Data$name_vdem -> rownames(QCA.Data)
```

## Calibration details


```{r, dpi=200}
rel.CAL <- plot(QCA.Data$al_religion2000, QCA.Data$qca.relfrag, xlab = "Religious fractionalization (raw)",
             ylab = "Religious fractionalization (fuzzy)", font = 2, font.lab = 2) +
  abline(v = 0.5, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

rel.CAL

gdp.CAL <- plot(QCA.Data$mad_gdppc, QCA.Data$qca.gdp, xlab = "GDP per capita (raw)",
                ylab = "GDP per capita (fuzzy)", font = 2, font.lab = 2) +
  abline(v = 12500, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

gdp.CAL

fog.CAL <- plot(QCA.Data$fh_fog, QCA.Data$qca.fog, xlab = "Functioning of Government (raw)",
                ylab = "Functioning of Government (fuzzy)", font = 2, font.lab = 2) +
  abline(v = 6.5, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

fog.CAL

supp.relgrp.CAL <- plot(QCA.Data$vdem_20ya_regsup_g7, QCA.Data$qca.supp.relgrp, xlab = "Support from religious groups (20 year average, raw)",
                        ylab = "Support from religious groups (20 year average, fuzzy)", font = 2, font.lab = 2) +
  abline(v = 0.12, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

supp.relgrp.CAL

supp.urbanwk.CAL <- plot(QCA.Data$vdem_20ya_regsup_g9, QCA.Data$qca.supp.urbanwk, xlab = "Support from urban working class and unions (20 year average, raw)",
                        ylab = "Support from urban working class and unions (20 year average, fuzzy)", font = 2, font.lab = 2) +
  abline(v = 0.12, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

supp.urbanwk.CAL


## outcome

rel.reg.CAL <- plot(QCA.Data$NXX2014X, QCA.Data$qca.outcome2.nxx, xlab = "Religious regulation - Mjority Rel. and all regulation (raw)",
                     ylab = "Religious regulation - Mjority Rel. and all regulation (fuzzy)", font = 2, font.lab = 2) +
  abline(v = 10.5, col = "black", lwd = 0.7) +
  abline(h = 0.5, col = "black", lwd = 0.7)

rel.reg.CAL

```


```{r}
### NCA - Necessary Condition Analysis
QCAfit(QCA.Data[, 17:22], QCA.Data$qca.outcome2.nxx, necessity = TRUE)
QCAfit(QCA.Data[, 17:22], 1 - QCA.Data$qca.outcome2.nxx, necessity = TRUE)
```


```{r, fig.height=7, fig.width=9, dpi=200}
### correlation of calibrated data
corrplot::corrplot.mixed(cor(QCA.Data[, c(17:23)]), lower="number", upper="circle", tl.pos = "lt")

corrplot::corrplot.mixed(cor(QCA.Data[, c(17:27)]), lower="number", upper="circle", tl.pos = "lt")


corrplot::corrplot.mixed(cor(QCA.Data[, c(5,7,8,11,12)]), lower="number", upper="circle", tl.pos = "lt")

```


## Truth table - positive outcome - pars. solution -- incl.cut = 0.75 

```{r}
TruthTable.pars <- truthTable(QCA.Data[, c(17:22,27)], outcome = "qca.outcome2.nxx", conditions = "qca.relfrag, qca.gdp, qca.srel1900, qca.fog, qca.supp.relgrp, qca.supp.urbanwk",
                    complete = TRUE, show.cases = TRUE, 
                    sort.by = c("incl", "n"), n.cut = 1, incl.cut = 0.75)
## show truth table
TruthTable.pars
```



### solution terms pars. sol. - positive 0.75

```{r}
solution.ps <- minimize(TruthTable.pars, outcome = "qca.outcome2.nxx", include = "1, ?", row.dom = TRUE,
                    details = TRUE, show.cases = TRUE, use.tilde = TRUE,
                    method = "CCubes")

## parsimonious solution (Table XX) 
solution.ps

solution.ps$PIchart
```




## -- TT parsimonious solution - incl.cut 0.9 -- Neg.outcome

```{r}
TruthTable.negout.pars <- truthTable(QCA.Data[, c(17:22,27)], outcome = "~qca.outcome2.nxx", conditions = "qca.relfrag, qca.gdp, qca.srel1900, qca.fog, qca.supp.relgrp, qca.supp.urbanwk",
                    complete = TRUE, show.cases = TRUE, 
                    sort.by = c("incl", "n"), n.cut = 1, incl.cut = 0.9)
## show truth table
TruthTable.negout.pars
```



### ----------


## Non-outcome parsimonious solution (0.9 inclusion)





```{r}
non.solution.ps <- minimize(TruthTable.negout.pars, outcome = "~qca.outcome2.nxx", include = "1, ?", row.dom = TRUE,
                    details = TRUE, show.cases = TRUE, use.tilde = TRUE,
                    method = "CCubes")

## parsimonious non-oucome solution (Table XX) 
non.solution.ps

non.solution.ps$PIchart
```




# ------------------------ CS
# ------------------------


### conservative solution - truth table - positive outcome - incl. 0.75

```{r}
TruthTable.cons <- truthTable(QCA.Data[, c(17:22,27)], outcome = "qca.outcome2.nxx", conditions = "qca.relfrag, qca.gdp, qca.srel1900, qca.fog, qca.supp.relgrp, qca.supp.urbanwk",
                    complete = FALSE, show.cases = TRUE, 
                    sort.by = c("incl", "n"), n.cut = 1, incl.cut = 0.75)

# Truth table conservative
TruthTable.cons
```

## conservative solution - positive outcome
## minimization

```{r}
solution.cs <- minimize(TruthTable.cons, outcome = "qca.outcome2.nxx",include = "1", row.dom = FALSE,
                    details = TRUE, show.cases = TRUE, use.tilde = TRUE,
                    all.sol = TRUE, method = "CCubes")

solution.cs

solution.cs$PIchart
```




## non-outcome - 0.9 - cons. solution


```{r}
TruthTable.negout.cons <- truthTable(QCA.Data[, c(17:22,27)], outcome = "~qca.outcome2.nxx", conditions = "qca.relfrag, qca.gdp, qca.srel1900, qca.fog, qca.supp.relgrp, qca.supp.urbanwk",
                    complete = FALSE, show.cases = TRUE, 
                    sort.by = c("incl", "n"), n.cut = 1, incl.cut = 0.9)

# Truth table conservative
TruthTable.negout.cons
```



```{r, eval=FALSE}
non.solution.cs <- minimize(TruthTable.negout.cons, outcome = "~qca.outcome2.nxx",include = "1", row.dom = FALSE,
                    details = TRUE, show.cases = TRUE, use.tilde = TRUE,
                    all.sol = TRUE, method = "CCubes")

non.solution.cs
```

## fin

```{r}
library(report)
report::report_packages()
```


```{r}
report::cite_packages()
```


